import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:awak/common/values/images.dart';
import 'package:awak/page/home/home_page.dart';
import 'package:awak/page/statistic/statistic_page.dart';
import 'package:awak/page/community/community_page.dart';
import 'package:awak/page/profile/profile_page.dart';
import 'package:awak/bloc/auth/auth_bloc.dart';
import 'package:awak/bloc/auth/auth_event.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

// TabIndex Bloc
class TabIndexState {
  final int index;
  TabIndexState(this.index);
}

class TabIndexCubit extends Cubit<TabIndexState> {
  TabIndexCubit() : super(TabIndexState(0));
  void setIndex(int idx) => emit(TabIndexState(idx));
}

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  @override
  void initState() {
    super.initState();
    // 检查当前登录用户
    context.read<AwakBloc>().add(CheckCurrentUserEvent());
  }

  @override
  Widget build(BuildContext context) {
    final List<Widget> _pages = const [
      HomePage(),
      StatisticPage(),
      CommunityPage(),
      ProfilePage(),
    ];
    final List<String> _icons = [
      AwakImages.home,
      AwakImages.statistics,
      AwakImages.community,
      AwakImages.profile,
    ];
    final List<String> _iconsActive = [
      AwakImages.homeActive,
      AwakImages.statisticsActive,
      AwakImages.communityActive,
      AwakImages.profileActive,
    ];
    return BlocBuilder<TabIndexCubit, TabIndexState>(
      builder: (context, tabState) {
        return Scaffold(
          backgroundColor: Colors.transparent,
          body: Stack(
            children: [
              IndexedStack(
                index: tabState.index,
                children: _pages,
              ),
              Align(
                alignment: Alignment.bottomCenter,
                child: Padding(
                  padding: EdgeInsets.only(bottom: 40.h),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(58.r),
                    child: BackdropFilter(
                      filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),
                      child: Container(
                        width: MediaQuery.of(context).size.width * 0.85.w,
                        height: 70.h,
                        decoration: BoxDecoration(
                          color: Colors.white.withOpacity(0.5),
                          borderRadius: BorderRadius.circular(58.r),
                          border: Border.all(color: Colors.white, width: 2.r),
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: List.generate(4 * 2 - 1, (i) {
                            if (i.isEven) {
                              final index = i ~/ 2;
                              final isActive = tabState.index == index;
                              return GestureDetector(
                                onTap: () {
                                  context.read<TabIndexCubit>().setIndex(index);
                                },
                                child: Image.asset(
                                  isActive ? _iconsActive[index] : _icons[index],
                                  width: 32.r,
                                  height: 32.r,
                                ),
                              );
                            } else {
                              return SizedBox(width: 60.w);
                            }
                          }),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}